/*This code replicates the results presented in Finke, Daniel (2020) "Regime Type and Co-Sponsorship in the UN General Assembly",****/
/*International Interactions, forthcoming. ****Please make sure that you have read the instructions in the readme.txt files first.***/



/*FIGURES 1*/
/*Replicate Figures 1: left pane with hrc resolutions*/
use "..\rawdata_assembly.dta" , replace
g democ_author= democ* author
g autoc_author = autoc*author
bysort year: egen n_demauthor= total( democ_author)
bysort year: egen n_autauthor= total( autoc_author)
bysort year: egen n_allauthor= total(author)
bysort symbol: g run=_n
replace run=0 if run >1
bysort year: egen n_drafts= total(run)
twoway (line n_allauthor year, lpattern(dot)) (line n_autauthor year, lpattern(dash))(line n_demauthor year, lpattern(shortdash))(line n_drafts year,yaxis(2))


/*Replicate Figures 1: right pane without hrc resolutions*/
use "..\rawdata_assembly.dta" , replace
drop if HRC ==1
g democ_author= democ* author
g autoc_author = autoc*author
bysort year: egen n_demauthor= total( democ_author)
bysort year: egen n_autauthor= total( autoc_author)
bysort year: egen n_allauthor= total(author)
bysort symbol: g run=_n
replace run=0 if run >1
bysort year: egen n_drafts= total(run)
twoway (line n_allauthor year, lpattern(dot)) (line n_autauthor year, lpattern(dash))(line n_demauthor year, lpattern(shortdash))(line n_drafts year,yaxis(2))


/*FIGURES 2*/
/*Replicate Figures 2: left pane with hrc resolutions*/
use "..\rawdata_assembly.dta" , replace
bysort symbol: g run=_n
replace run=0 if run >1
bysort year: egen n_drafts= total(run)
bysort year topic: egen n_drafts_topic= total(run)
twoway (line n_drafts year)  (line n_drafts_topic year if topic==1, lpattern(dash))  (line n_drafts_topic year if topic==2, lpattern(dot))  (line n_drafts_topic year if topic==3, lpattern(shortdash)) (line n_drafts_topic year if topic==4, lpattern(dash_dot))  (line n_drafts_topic year if topic==5, lpattern(shortdash_dot))


/*Replicate Figures 2: right pane without hrc resolutions*/
use "..\rawdata_assembly.dta" , replace
drop if HRC ==1
bysort symbol: g run=_n
replace run=0 if run >1
bysort year: egen n_drafts= total(run)
bysort year topic: egen n_drafts_topic= total(run)
twoway (line n_drafts year)  (line n_drafts_topic year if topic==1, lpattern(dash))  (line n_drafts_topic year if topic==2, lpattern(dot))  (line n_drafts_topic year if topic==3, lpattern(shortdash)) (line n_drafts_topic year if topic==4, lpattern(dash_dot))  (line n_drafts_topic year if topic==5, lpattern(shortdash_dot))


/*FIGURES 3*/
/*All resolutions (upper left figure)*/
use "..\withtopics.dta", replace
collapse (sum) S Shrc (mean)  logtottrade  ccode1 ccode2 polkat, by(year dycode)
bysort year: egen demtot=total(S) if polkat ==2
bysort year: egen demall=total(S) if polkat ==2 |polkat==0
bysort year: egen auttot=total(S) if polkat ==1
bysort year: egen autall=total(S) if polkat ==1 |polkat==0
g avgdem=demtot/demall
g avgaut=auttot/autall
twoway(connect avgdem year) (connect avgaut year)

/*Figure 3 for individual topics*/
use "..\withtopics.dta", replace
bysort year topic: egen demtot=total(S) if polkat ==2
bysort year topic: egen demall=total(S) if polkat ==2 |polkat==0
bysort year topic: egen auttot=total(S) if polkat ==1
bysort year topic: egen autall=total(S) if polkat ==1 |polkat==0
g avgdem=demtot/demall
g avgaut=auttot/autall

/*Social/Health*/
twoway(connect avgdem year if topic==1) (connect avgaut year if topic==1)
/*Climate/Environment*/
twoway(connect avgdem year if topic==2) (connect avgaut year if topic==2)
/*Religious/Ethnic Conflicts*/
twoway(connect avgdem year if topic==3) (connect avgaut year if topic==3)
/*Organized Crime*/
twoway(connect avgdem year if topic==4) (connect avgaut year if topic==4)
/*National Security*/
twoway(connect avgdem year if topic==5) (connect avgaut year if topic==5)


/*FIGURES 4 based on Regression Analysis presented in APPENDIX A1 (!Estimation and Calculation of Margins may take several hours!)*/

use "..\withtopics.dta", replace
collapse (sum) S Shrc (mean)  logtottrade  ccode1 ccode2 polkat, by(year dycode)

/*Exclude Drafts proposed in the HRC*/
g S2= S-Shrc

/*Column 1: All Resolutions w.o. Dyad-FE*/
ppmlhdfe S2 i.polkat##i.year logtottrade, absorb(ccode1 ccode2) d
outreg2 using A1.doc, replace 
margins , dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Column 2: All Resolutions w.o. Dyad-FE*/
ppmlhdfe S2 i.polkat##i.year logtottrade, absorb(dycode ccode1 ccode2) d
outreg2 using A1.doc, append
margins , dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Column 3: Social/Health*/
use "..\withtopics.dta", replace
/*Exclude Drafts proposed in the HRC*/
g S2= S-Shrc

ppmlhdfe S2 i.polkat##i.year logtottrade  if topic==1, absorb(dycode ccode1 ccode2) d
outreg2 using A1.doc, append
margins if topic==1, dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Column 4: Climate/Environment*/
ppmlhdfe S2 i.polkat##i.year logtottrade  if topic==2, absorb(dycode ccode1 ccode2) d
outreg2 using A1.doc, append
margins if topic==2, dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Column 5: Religious/Ethnic Conflicts*/
ppmlhdfe S2 i.polkat##i.year logtottrade  if topic==3, absorb(dycode ccode1 ccode2) d
outreg2 using A1.doc, append
margins if topic==3, dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Column 6: Organized Crime*/
ppmlhdfe S2 i.polkat##i.year logtottrade  if topic==4, absorb(dycode ccode1 ccode2) d
outreg2 using A1.doc, append
margins if topic==4, dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Column 7: National Security*/
ppmlhdfe S2 i.polkat##i.year logtottrade  if topic==5, absorb(dycode ccode1 ccode2) d
outreg2 using A1.doc, append
margins if topic==5, dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)


/***********************************************************************************************************/
/***************************APPENDICES A2 & A3**************************************************************/
/***********************************************************************************************************/

use "..\withtopics.dta", replace
collapse (sum) S Shrc (mean)  logtottrade  ccode1 ccode2 polkat, by(year dycode)

/*Exclude Drafts proposed in the HRC*/
g S2= S-Shrc

/*Table A2, Model 1: Random effects in Random Effects in Dyads & Fixed Effects in Countries*/
xtset dycode
xtpoisson S2 i.polkat##i.year logtottrade i.ccode1 i.ccode2, re
outreg2 using A2.doc, replace
margins , dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck 
marginsplot, recast(line) recastci(rarea)

/*Table A2, Model 2: Fixed Effects in Dyads only*/
ppmlhdfe S2 i.polkat##i.year logtottrade , absorb(dycode) d
outreg2 using A2.doc, append
margins , dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Table A2, Model 3: Full Model without Bilateral Trade (->fewer missings)*/
ppmlhdfe S2 i.polkat##i.year , absorb(dycode ccode1 ccode2) d
outreg2 using A2.doc, append
margins , dydx(i.polkat) at(year=(1994(1)2016) ) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)


/*Table A2, Model 4: Effect of Regime Type for Stables and Unstable States*/
bysort dycode: egen sd_polkat= sd(polkat)
g polstable= 0
replace polstable = 1 if sd_polkat >0
drop sd_polkat
ppmlhdfe S2 i.polstable##i.polkat##i.year logtottrade , absorb(dycode ccode1 ccode2) d
outreg2 using A2.doc, append
margins , dydx(i.polkat) at(year=(1994(1)2016) polstable=(0)) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)
margins , dydx(i.polkat) at(year=(1994(1)2016) polstable=(1)) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

/*Table A2, Model 5: Effect of Regime Type for Stables and Unstable States*/
sort dycode year
bysort dycode: g polkatL1=polkat[_n-1]
g yearX= 2222
replace yearX= year if polkatL1 != polkat & polkatL1!=. & polkat!=.
bysort dycode: egen minyear= min(yearX)
g polchange =0
replace polchange =1 if year >=minyear
ppmlhdfe S2 i.polchange##i.polkat##i.year logtottrade , absorb(dycode ccode1 ccode2) d
outreg2 using A2.doc, append
margins , dydx(i.polkat) at(year=(1994(1)2016) polchange=(0)) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)
margins , dydx(i.polkat) at(year=(1994(1)2016) polchange=(1)) level(99) noestimcheck predict(mu)
marginsplot, recast(line) recastci(rarea)

